package com.bl.student.manager.controller.goods;


import com.bl.student.common.ApiResponseBody;
import com.bl.student.dao.model.GoodsEntity;
import com.bl.student.manager.controller.BaseController;
import com.bl.student.service.biz.goods.GoodsService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.goods.GoodsAddVo;
import com.bl.student.service.vo.goods.GoodsListVo;
import com.bl.student.service.vo.goods.GoodsModVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liangweikang on 18/1/29.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/manager/goods"})
@Api(value = "商品", description = "商品相关api")
public class GoodsController extends BaseController {

    @Autowired
    GoodsService goodsService;

    @ApiOperation(value = "商品列表", notes = "商品列表 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_list"})
    public ApiResponseBody<GoodsListVo> goodsList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        GoodsListVo goodsListVo = goodsService.getAllGoodsList(pageable);

        return ApiResponseBody.createSuccessBody(goodsListVo);
    }

    @ApiOperation(value = "商品详情", notes = "商品详情 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_detail"})
    public ApiResponseBody<GoodsEntity> goodsDetail(
            @RequestParam(value = "id", required = true) String id,
            HttpServletRequest request) {


        GoodsEntity goodsEntity = goodsService.getGoodDetail(id);

        return ApiResponseBody.createSuccessBody(goodsEntity);
    }

    @ApiOperation(value = "删除批量", notes = "删除批量艺考信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_list"})
    public ApiResponseBody delList(
            @Valid @RequestBody DelListBody body, BindingResult result,
            HttpServletRequest request) {

        goodsService.delList(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除单个商品信息", notes = "删除单个商品信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {


        goodsService.del(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "增加商品信息", notes = "增加商品信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add"})
    public ApiResponseBody add(
            @Valid @RequestBody GoodsAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        goodsService.add(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改商品信息", notes = "修改商品信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody modify(
            @Valid @RequestBody GoodsModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        goodsService.modify(body);

        return ApiResponseBody.createSuccessBody(null);
    }
}
